package com.jblv.frame.demo.kafka;

import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.stereotype.Component;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author happyonion.yangtao
 * @version 1.0
 * @title InitListener
 * @projectName jblv-frame
 * @packageName com.jblv.user.service.kafka
 * @description
 * @date Create at 2020/4/17 14:27
 * @since jdk1.8
 */
@SuppressWarnings("AlibabaThreadPoolCreation")
@Component
public class InitListener implements ApplicationListener {

    @Override
    public void onApplicationEvent(ApplicationEvent event) {

        if (event instanceof ApplicationEnvironmentPreparedEvent) { // 初始化环境变量
        } else if (event instanceof ApplicationPreparedEvent) { // 初始化完成
        } else if (event instanceof ContextRefreshedEvent) { // 应用刷新
        } else if (event instanceof ApplicationReadyEvent) {// 应用已启动完成
            //启动监听
            ExecutorService threadPool = Executors.newCachedThreadPool();
            threadPool.execute(() -> {
                MsgQueueListener.start();
                System.out.println("启动kafka消息监听...");
            });
        } else if (event instanceof ContextStartedEvent) { // 应用启动，需要在代码动态添加监听器才可捕获
        } else if (event instanceof ContextStoppedEvent) { // 应用停止
        } else if (event instanceof ContextClosedEvent) { // 应用关闭
        } else {
        }


    }

}